﻿using System;

namespace Picnic
{
    /// <summary>
    /// Represents a base class for disposable objects.
    /// </summary>
    public class DisposableObject : IDisposable
    {
        private bool _disposed;

        ~DisposableObject()
        {
            DisposeInternal(false);
        }

        /// <summary>
        /// Disposes this object.
        /// </summary>
        public void Dispose()
        {
            DisposeInternal(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Disposes object with specified context.
        /// </summary>
        /// <param name="calledExplicitly">
        /// If true, destroy managed object; otherwise unmanaged objects.
        /// </param>
        protected virtual void Dispose(bool calledExplicitly)
        {
        }

        private void DisposeInternal(bool calledExplicitly)
        {
            if (!_disposed)
            {
                Dispose(calledExplicitly);
            }

            _disposed = true;
        }
    }
}
